//
//  ST_DTChatCell.swift
//  DrinkTogether
//
//  Created by DouLei on 2018/6/25.
//  Copyright © 2018 DouLei. All rights reserved.
//

import UIKit
import SwifterSwift

class ST_DTChatCell: UITableViewCell {

    
//    var pushUserClosure:(()->())?
    
    var uuid = ""
    
    static let uploadErrorBtnSize = CGSize.init(width: 26, height: 26)
    
    lazy var icon:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.cornerRadius = DTChatUserImageWidth/2.0
        control.borderColor = Color.init(hexString: "#F2F4F7")
        control.borderWidth = 2
        control.imageView?.contentMode = .scaleAspectFill
        control.clipsToBounds = true
        
        control.add_touch_up(insideTarget: self, action: #selector(push_user_center))
        
        return control
    }()
    
    lazy var upload_error_btn:UIButton = {
        
        let control = UIButton.init(type: .custom)
        control.setImageForAllStates(ST_ImageUtils.imageName("n_私信_icon_发送失败")!)
        
        control.add_touch_up(insideTarget: self, action: #selector(re_send_message))
        
        control.isHidden = true
        return control
    }()
    
    
    lazy var uploadIndicator:UIActivityIndicatorView = {
        
        let control = UIActivityIndicatorView()
        control.color = .gray
        return control
    }()
    
    
    
    
    @objc func re_send_message(){
        
//        if let vc  = self.ViewController as?  DTChatViewController{
//            vc.re_send_message(cell: self)
//        }
    }
    
    lazy var icon_supplement_image_view:UIImageView = {
        let control = UIImageView()
       return control
    }()
    
    var icon_supplement_u_r_l:URL?{
        didSet{
            
            guard let url = icon_supplement_u_r_l else { return }
            
            icon_supplement_image_view.pin_setImage(from:url)
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.contentView.backgroundColor = Color.white
        
        self.selectionStyle = .none
        
        let longPressRecognizer = UILongPressGestureRecognizer.init(target: self, action: #selector(long_pressed(sender:)))
        
        self.contentView.addGestureRecognizer(longPressRecognizer)
        
        self.commonInit()
    }
    
    func commonInit(){
    
        self.contentView.addSubview(icon)
        self.contentView.addSubview(icon_supplement_image_view)
        self.addSubview(icon_supplement_image_view)
        icon_supplement_image_view.snp.makeConstraints { (make) in
            make.center.equalTo(icon)
            make.width.height.equalTo(52)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var message:ST_DTChatMessage!{
        didSet{
            
            icon_supplement_image_view.isHidden = message.direction == .send
            
            
            setting_uplaod_status()
        }
    }

    
    
    var push_user_centerClosure:(()->())?
    
    @objc func push_user_center(){
        
//            self.pushUserClosure?()
        
        if ASSISTANT_SET.contains(message.chatID){
            return
        }
        
        push_user_centerClosure?()
        
    }
    
    func setting_uplaod_status() {
        
    }
    
    @objc func long_pressed(sender: UILongPressGestureRecognizer)
    {
        
        if sender.state == .began {
            
            if let vc = self.parentViewController as? ST_PrivateChatViewController{
                vc.long_pressed(cell: self)
            }
            
        }
        
        
    }
}
